package com.zipeng.system.permission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zipeng.result.ResultUtils;
import com.zipeng.result.ResultVo;
import com.zipeng.system.permission.entity.Permission;
import com.zipeng.system.permission.service.PermissionService;
import com.zipeng.system.permission.vo.MakeMenuTree;
import com.zipeng.system.permission.vo.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * 根据id删除权限
     * @return
     */
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @RequestMapping(value = "deleteById/{id}",method = RequestMethod.DELETE)
    public ResultVo deleteById(@PathVariable("id") Long id){
        // 判断待删除权限是否有子权限
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().eq(Permission::getParentId, id);
        List<Permission> list = permissionService.list(query);
        if(!list.isEmpty()){
            return ResultUtils.error("不可删除含有子权限的权限!");
        }
        boolean b = permissionService.removeById(id);
        if(b){
            return ResultUtils.success("删除成功!");
        }else{
            return ResultUtils.error("删除失败!");
        }
    }

    /**
     * 编辑权限保存
     * @return
     */
    @PreAuthorize("hasAuthority('sys:permission:edit')")
    @RequestMapping(value = "editSave",method = RequestMethod.POST)
    public ResultVo editSave(@Valid @RequestBody Permission permission){
        boolean b = permissionService.updateById(permission);
        if(b){
            return ResultUtils.success("编辑成功!");
        }else{
            return ResultUtils.error("编辑失败!");
        }
    }

    /**
     * 根据id查询编辑的权限
     * @return
     */
    @RequestMapping(value = "getPermissionById/{id}",method = RequestMethod.GET)
    public ResultVo getPermissionById(@PathVariable("id") Long id){
        Permission res = permissionService.getById(id);
        return ResultUtils.success("查询成功",res);

    }

    /**
     * addPermission
     * @return
     */
    @PreAuthorize("hasAuthority('sys:permission:add')")
    @RequestMapping(value = "/addPermission",method = RequestMethod.POST)
    public ResultVo addPermission(@Valid @RequestBody Permission permission){
        boolean b = permissionService.save(permission);
        if(b){
            return ResultUtils.success("新增权限成功!");
        }else{
            return ResultUtils.error("新增权限失败！");
        }

    }

    /**
     * 获取权限列表数据
     * @return
     */
    @RequestMapping(value = "/getMenuList",method = RequestMethod.GET)
    public ResultVo getMenuList(){
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().orderByAsc(Permission::getOrderNum);
        List<Permission> list = permissionService.list();
        List<Permission> menuList = null;
        if(list.size() > 0){
            menuList =  MakeMenuTree.makeTree(list,0L);
        }
        return ResultUtils.success("查询列表成功",menuList);
    }

    /**
     * 新增权限获取上级树数据
     * @return
     */
    @RequestMapping(value = "getParenList",method = RequestMethod.GET)
    public ResultVo getParenList(){
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().eq(Permission::getType,"0").or().eq(Permission::getType,"1");
        List<Permission> list = permissionService.list(query);
        List<TreeVo> listTree = new ArrayList<>();
        TreeVo parentTree = new TreeVo();
        parentTree.setId(0L);
        parentTree.setPid(-1L);
        parentTree.setName("顶级菜单");;
        parentTree.setOpen(true);
        parentTree.setChecked(false);
        listTree.add(parentTree);
        if(list.size() > 0){
            for(Permission p : list){
                if(p != null){
                    TreeVo vo = new TreeVo();
                    vo.setId(p.getId());
                    vo.setPid(p.getParentId());
                    vo.setName(p.getLabel());
                    vo.setOpen(true);
                    vo.setChecked(false);
                    listTree.add(vo);
                }
            }
        }
        return ResultUtils.success("成功",listTree);
    }
}
